package com.welsy.iter.command
{
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.welsy.iter.business.*;
	import com.welsy.iter.vo.*;
	import com.welsy.iter.model.IterModelLocator;
	import com.welsy.util.Comparator;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.controls.Alert;
	import mx.collections.ICollectionView;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.utils.ArrayUtil;
	import com.welsy.iter.vo.iter.ModuleVO;
	import com.welsy.iter.vo.iter.ElementVO;
	import com.welsy.iter.vo.iter.pattern.IterState;
	import com.welsy.iter.event.PublishIterEvent;
	import com.welsy.iter.vo.iter.pattern.IterState;
	import mx.collections.ArrayCollection;
	import com.welsy.classroom.model.ClassModelLocator;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.welsy.iter.event.GetItersEvent;	
	
	/**
	 * @version	$Revision: 1.1 $
	 */
	public class PublishIterCommand implements ICommand, IResponder
	{
	  	public function PublishIterCommand()
		{	 
		}
	
		public function execute( event : CairngormEvent ): void
		{
			var IterEvent:PublishIterEvent = PublishIterEvent( event );
			
			var delegate:PublishIterDelegate = new PublishIterDelegate( this );
			delegate.publishIter(IterEvent.iterID);			    			
		}
	
		public function result( event : Object ) : void
		{				
			var model : IterModelLocator = IterModelLocator.getInstance();
			
			var resultlist:XMLList = XMLList(event.result)
			var result:String = XML(resultlist.child(0).child(0)).toString()
			
			if (result == "ok") {   		
				model.selectedIter.iter.state = IterState.publish;
				Alert.show("Iter pubblicato");
				
				var gis_eve : GetItersEvent = new GetItersEvent;
				CairngormEventDispatcher.getInstance().dispatchEvent(gis_eve);			
			}
			else
				Alert.show("Iter non pubblicato")
		}
		
		public function changeStatusIters(model:IterModelLocator):void
		{
			//forse da togliere tranne l'ultima riga
			/* var acIters:ArrayCollection = ArrayCollection(model.iters)
			var myItem:IterVO
			for each (myItem in acIters) {
				if (myItem.id == model.selectedIter.iter.id){
					myItem.state = IterState.publish;
					model.isPublishIter = true;
					break;
				}    				
			} */		
		}
				
		public function fault( event : Object ) : void
		{
			var faultEvent : FaultEvent = FaultEvent( event );
			Alert.show("Iter could not be retrieved!" );
		}
	}

}